package com.example.springbootdemo.event;

import com.example.springbootdemo.dto.SmsDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  短信事件监听
 * </p>
 *
 * @author yurenwei
 * @since 2023/9/14
 */
@Slf4j
@Component
public class SmsListener implements ApplicationListener<SmsEvent> {

    @Async
    @Override
    public void onApplicationEvent(SmsEvent smsEvent) {
        log.info("执行短信发送事件监听开始");
        log.info("监听到事件名称:{}",smsEvent.getEventName());

        SmsDTO sms = smsEvent.getSms();
        log.info("监听到发送短信参数:姓名：{}，手机号：{}，消息：{}",sms.getUserName(),sms.getPhone(),sms.getMsg());
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("执行短信发送事件监听结束");
    }
}
